package com.caine.androidtask.ls27;




import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class BackLight extends Activity implements OnClickListener {

    private Button mBtnLcdON;

    private Button mBtnLcdOFF;

    private Button mBtOk;

    private Button mBtFailed;

    private String lcdCmdON = "echo 250 > /sys/class/leds/lcd-backlight/brightness";

    private String lcdCmdOFF = "echo 50 > /sys/class/leds/lcd-backlight/brightness";

    private final int ERR_OK = 0;

    private final int ERR_ERR = 1;
    
    private int mOldBrightness;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
			mOldBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {
			// TODO Auto-generated catch block
			mOldBrightness = 255;
			e.printStackTrace();
		}
        //setContentView(R.layout.backlight);

        //mBtnLcdON = (Button) findViewById(R.id.Display_lcd_on);
       // mBtnLcdOFF = (Button) findViewById(R.id.Display_lcd_off);
       // mBtOk = (Button) findViewById(R.id.display_bt_ok);
       // mBtFailed = (Button) findViewById(R.id.display_bt_failed);

        mBtnLcdON.setOnClickListener(this);
        mBtnLcdOFF.setOnClickListener(this);
       // mBtOk.setOnClickListener(this);
        //mBtFailed.setOnClickListener(this);
       // mSp = getSharedPreferences("FactoryMode", Context.MODE_PRIVATE);
    }

    private void setLastError(int err) {
        System.out.print(err);
    }

    public void onClick(View arg0) {
        try {

            if (arg0.getId() == mBtnLcdON.getId()) {
                String[] cmd = {
                        "/system/bin/sh", "-c", lcdCmdON
                };
                int ret = ShellExe.execCommand(cmd);
                if (0 == ret) {
                    setLastError(ERR_OK);
                } else {
                    setLastError(ERR_ERR);
                }

            } else if (arg0.getId() == mBtnLcdOFF.getId()) {
                String[] cmd = {
                        "/system/bin/sh", "-c", lcdCmdOFF
                };
                int ret = ShellExe.execCommand(cmd);
                if (0 == ret) {
                    setLastError(ERR_OK);
                } else {
                    setLastError(ERR_ERR);
                }

            } else if (arg0.getId() == mBtOk.getId()) {
                finish();
            } else if (arg0.getId() == mBtFailed.getId()) {
                finish();
            }
        } catch (IOException e) {
            setLastError(ERR_ERR);
        }
    }
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	 Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS,mOldBrightness);
    	 //IPowerManager pManager = IPowerManager.Stub.asInterface(ServiceManager("power"));
    	
    }
}
